package controleur;

import java.util.Properties;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.hibernate.HibernateException;

/**
 * Servlet implementation class Servlet
 */
public class Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public Servlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		System.out.println("GET");

		getServletContext().getRequestDispatcher("/consulterDRP.jsp").forward(
				request, response);
		return;

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		System.out.println("POST");

		// Calculer Reapprovisionnement
		if (request.getParameter("button")
				.equals("CalculerReapprovisionnement")) {
			Fonction_Calcul c = new Fonction_Calcul();
		Date date = null;
		Date d = new Date();
		d.setDate(8);
			while (true) {
				date = new Date();
				if ( c.IsDay(date) && c.IsTime(date) && !c.TempsInterdit(d) && d.getDay() != date.getDay())
				{
					
					try {
						
						CalculGeneral g = new CalculGeneral();
						g.calculG();
						d = date;
						
					} catch (HibernateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				
			
				
				
				
				
				/*
				 * Timer t = new Timer(); System.out.println("avant timer");
				 * 
				 * t.scheduleAtFixedRate(new Tache(), 1, 86400000);
				 * 
				 * getServletContext().getRequestDispatcher("/reponse.jsp").forward
				 * ( request, response); return;
				 */

			}
		}

		// Consulter Reapprovisionnement
		if (request.getParameter("button").equals(
				"ConsulterReapprovisionnement")) {
			Traiter_Donnees donnees = new Traiter_Donnees();

			try {
				request.setAttribute("lignesCommande",
						donnees.recuperer_LignesCommande());
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			getServletContext().getRequestDispatcher("/consulterDRP.jsp")
					.forward(request, response);
			return;

		}

	}

}
